package com.zhongwang.mms.module.buy.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.module.buy.model.ShoppingPlanModel;
import com.zhongwang.mms.module.buy.service.EditBuyPlanService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description: 采购计划编辑
 * 查看 -> buy:buyedit:view
 * 编辑、提交、合报 -> buy:buyedit:edit
 * 导入、导出 -> buy:buyexport:add
 * 删除 -> buy:buyedit:del
 * 变更 -> buy:buyedit:alter
 *
 * @Author: China.sgl
 * @Date: 2020/4/7 14:42
 */
@SuppressWarnings("ALL")
@Slf4j
@Controller
@RequestMapping("/buy/editBuyPlan")
public class EditBuyPlanController extends BaseController {

    @Autowired
    private EditBuyPlanService editBuyPlanService;

    /**
     * 采购计划编辑页面
     *
     * @return 路径
     */
    @GetMapping
    @RequiresPermissions({"buy:buyedit:view"})
    public String project(Model model) {
        model.addAttribute("stocks", editBuyPlanService.findStocks());
        return "buy/editBuyPlan/page";
    }

    /**
     * 采购计划列表数据
     *
     * @param shoppingPlanModel 查询条件
     * @return list
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"buy:buyedit:view"})
    public JsonResult list(ShoppingPlanModel shoppingPlanModel) {
        long start = System.currentTimeMillis();
        startPage();
        List<ShoppingPlanModel> list = editBuyPlanService.findBuyPlanByQuery(shoppingPlanModel);
        log.debug("采购计划编辑 -> 列表数据" + (System.currentTimeMillis() - start) + "毫秒");
        return jsonResult(list);
    }

    /**
     * 查询采购计划
     *
     * @param sppId 采购计划主键
     * @param rppId 提报计划主键
     * @return json
     */
    @ResponseBody
    @PostMapping("/id")
    @RequiresPermissions({"buy:buyedit:view"})
    public ShoppingPlanModel id(Integer sppId, Integer rppId) {
        return editBuyPlanService.findBuyPlanById(sppId, rppId);
    }

    /**
     * 修改采购计划
     *
     * @param reportPlan reportPlan
     * @return json
     */
    @ResponseBody
    @PostMapping("/edit")
    @RequiresPermissions({"buy:buyedit:edit"})
    public JsonResult updateProject(ShoppingPlanModel shoppingPlanModel) {
        return jsonResult(editBuyPlanService.update(shoppingPlanModel));
    }

    /**
     * 删除采购计划
     *
     * @param id 主键
     * @return json
     */
    @ResponseBody
    @PostMapping("/delete")
    @RequiresPermissions({"buy:buyedit:del"})
    public JsonResult delete(Integer id) {
        return jsonResult(editBuyPlanService.delete(id));
    }

    /**
     * 提交采购计划
     *
     * @param id 主键
     * @return json
     */
    @ResponseBody
    @PostMapping("/commit")
    @RequiresPermissions({"buy:buyedit:edit"})
    public JsonResult commit(Integer id) {
        return jsonResult(editBuyPlanService.commitById(id));
    }
    /**
     * 撤销采购计划
     *
     * @param id 主键
     * @return json
     */
    @ResponseBody
    @PostMapping("/cancel")
    @RequiresPermissions({"buy:buyedit:edit"})
    public JsonResult cancel(Integer sppId) {
        return jsonResult(editBuyPlanService.cancelById(sppId));
    }

    /**
     * 撤销采购计划
     *
     * @param id 主键
     * @return json
     */
    @ResponseBody
    @PostMapping("/cancels")
    @RequiresPermissions({"buy:buyedit:edit"})
    public JsonResult cancels(Integer[] sppId) {
        return jsonResult(editBuyPlanService.cancelByIds(sppId));
    }

    /**
     * 批量提交采购计划
     *
     * @param id 主键
     * @return json
     */
    @ResponseBody
    @PostMapping("/commitAll")
    @RequiresPermissions({"buy:buyedit:edit"})
    public JsonResult commit(Integer[] rppIds) {
        return jsonResult(editBuyPlanService.commitInId(rppIds));
    }

    /**
     * 合报采购计划
     *
     * @param ids 主键
     * @param num 数量和
     * @return json
     */
    @ResponseBody
    @PostMapping("/merge")
    @RequiresPermissions({"buy:buyedit:edit"})
    public JsonResult merge(Integer id, Integer[] ids, Double num) {
        return jsonResult(editBuyPlanService.merge(id, ids, num));
    }

    /**
     * 采购计划导出
     *
     * @return JsonResult
     */
    @ResponseBody
    @GetMapping("/export")
    @RequiresPermissions({"buy:buyexport:add"})
    public void export(HttpServletResponse response) {
        editBuyPlanService.export(response);
    }

    /**
     * 采购计划导入
     *
     * @param file      导入文件
     * @return JsonResult
     */
    @ResponseBody
    @PostMapping("/import")
    @RequiresPermissions({"buy:buyexport:add"})
    public JsonResult buyPlanImport(@RequestParam("file") MultipartFile file) {
        return success(editBuyPlanService.importFile(file));
    }

    /**
     * 判断文件是否存在
     *
     * @param url 文件路径
     * @return
     */
    @ResponseBody
    @PostMapping("/exist")
    @RequiresPermissions({"buy:buyedit:view"})
    public JsonResult exist(String url) {
        return jsonResult(editBuyPlanService.exist(url));
    }

    /**
     * 采购计划导出
     *
     */
    @ResponseBody
    @GetMapping("/export/change")
    @RequiresPermissions({"buy:buyexport:add"})
    public void changeExport(ShoppingPlanModel shoppingPlanModel, HttpServletResponse response) {
        editBuyPlanService.changeExport(shoppingPlanModel, response);
    }
}
